package cn.edu.jnu.finalproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class InputBookItemActivity extends AppCompatActivity {

    public static final int RESULT_CODE_SUCCESS = 666;
    private int position;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_book_item);

        position = this.getIntent().getIntExtra("position",0);
        String title = this.getIntent().getStringExtra("title");
        String author = this.getIntent().getStringExtra("author");
        String translator = this.getIntent().getStringExtra("translator");
        String publisher = this.getIntent().getStringExtra("publisher");
        int pubDate1 = this.getIntent().getIntExtra("pubDate1",0);
        int pubDate2 = this.getIntent().getIntExtra("pubDate2",0);
        String ISBN = this.getIntent().getStringExtra("ISBN");


        EditText editTextTitle = findViewById(R.id.edit_Text_Book_Item_Title);
        EditText editTextAuthor = findViewById(R.id.edit_Text_Book_Item_Author);
        EditText editTextTranslator = findViewById(R.id.edit_Text_Book_Item_Translator);
        EditText editTextPublisher = findViewById(R.id.edit_Text_Book_Item_Publisher);
        EditText editTextPubDate1 = findViewById(R.id.edit_Text_Book_Item_PubDate1);
        EditText editTextPubDate2 = findViewById(R.id.edit_Text_Book_Item_PubDate2);
        EditText editTextISBN = findViewById(R.id.edit_Text_Book_Item_ISBN);

        if(null != title){
            editTextTitle.setText(title);
            editTextAuthor.setText(author);
            editTextTranslator.setText(translator);
            editTextPublisher.setText(publisher);
            editTextPubDate1.setText(Integer.toString(pubDate1));
            editTextPubDate2.setText(Integer.toString(pubDate2));
            editTextISBN.setText(ISBN);
        }


        Button buttonOk = this.findViewById(R.id.button_ok);
        buttonOk.setOnClickListener(view -> {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("title",editTextTitle.getText().toString());
            bundle.putString("author",editTextAuthor.getText().toString());
            bundle.putString("publisher",editTextPublisher.getText().toString());
            bundle.putString("translator",editTextTranslator.getText().toString());
            bundle.putInt("pubDate1",Integer.parseInt(editTextPubDate1.getText().toString()));
            bundle.putInt("pubDate2",Integer.parseInt(editTextPubDate2.getText().toString()));
            bundle.putString("ISBN",editTextISBN.getText().toString());
            bundle.putInt("position",position);

            intent.putExtras(bundle);
            setResult(RESULT_CODE_SUCCESS,intent);
            InputBookItemActivity.this.finish();
        });

    }
}